#!/bin/bash -e

ROOT=$(realpath $(dirname $0)/..)
INSTDIR=$ROOT/inst
local_config=$ROOT/local/system.conf
postfix=
intf_mac=9a:02:57:1e:8f:00
create_local=
docker_args=
networking=
alt_faux=
image=daqf/faux1
build=

cd $ROOT
source misc/config_base.sh

# If the argument is -n then create a networking container
if [ "$1" == "-n" ]; then
    networking=$1
    shift
fi

if [ "$1" == "-b" ]; then
    build=$1
    shift
fi

# If the argument is :lo, then create a local IP address.
if [ "$1" == ":lo" ]; then
    create_local=y
    shift
fi

# If the argument starts with a :, then it's an interface name.
if [ "${1#:}" != "$1" ]; then
    realintf=${1#:}
    postfix=-$realintf
    echo Running using interface $realintf
    shift
fi

# If the first argument starts with @, then it's a postfix designator.
if [ "${1#@}" != "$1" ]; then
    postfix=-${1#@}
    shift
fi

# If the first argument is a number, then it's a port designation.
if [ $# -gt 0 -a $(($1)) -gt 0 ]; then
    port_num=$1
    postfix=-$port_num
    intf_mac=9a:02:57:1e:8f:0$port_num
    shift
fi

if [ "$1" == "alt" ]; then
    image=daqf/faux2
    shift
fi

if [ -n "$networking" ]; then
    container_prefix=daq-networking
    image=daqf/networking
    net_opt=
else
    container_prefix=daq-faux
    net_opt=--net=none
fi

if [ -n "$build" ]; then
    DAQ_TARGETS=${image#daqf/} bin/docker_build force
fi

FAUX_ARGS="$*"

CONTAINER=$container_prefix$postfix

if [ -z "$FAUX_ARGS" ]; then
    FAUX_ARGS="$faux_args"
fi

echo Launching faux $FAUX_ARGS...

if docker container inspect $CONTAINER --format '{{ .Name }}' > /dev/null 2>&1; then
    echo -n "Clensing old container "
    docker rm -f $CONTAINER
fi

FAUXBASE=$INSTDIR/faux/$CONTAINER

FAUXTMP=$FAUXBASE/tmp
sudo mkdir -p $FAUXTMP
docker_args+=" -v $FAUXTMP:/tmp"

FAUXLOCAL=$FAUXBASE/local
sudo mkdir -p $FAUXLOCAL
docker_args+=" -v $FAUXLOCAL:/root/local"

CONFIG_DIR=$ROOT/${test_config%/}/$CONTAINER
if [ -d "$CONFIG_DIR" ]; then
    echo Mapping config dir $CONFIG_DIR...
    docker_args+=" -v $CONFIG_DIR:/config/start"
elif [ -n "$test_config" ]; then
    echo Config dir $CONFIG_DIR not found.
fi

docker_args="$docker_args -e LOCAL_IF=$realintf"

cid=$(docker run -d --privileged --name $CONTAINER --hostname $CONTAINER $net_opt $docker_args $image $FAUX_ARGS)
pid=$(docker inspect --format="{{ .State.Pid }}" $cid)

echo DAQ autoclean docker kill $CONTAINER

if [ -n "$realintf" ]; then
    echo Moving $realintf into $pid...
    sudo ip link set $realintf netns $pid
    docker exec $CONTAINER ip link set $realintf up
    docker exec $CONTAINER ip link show $realintf
else
    intf=faux$postfix
    echo Removing old interface $intf
    sudo ip link del $intf 2>/dev/null || true

    echo Adding new interface to $pid...
    sudo ip link add $intf type veth peer name faux-eth0 addr $intf_mac netns $pid
    sudo ip link set $intf up
fi

if [ -n "$create_local" ]; then
    sudo ip addr add 10.20.0.1/16 broadcast 10.20.255.255 dev $intf
    sudo ip addr show dev $intf
fi

echo Done with faux device launch.
